// src/controllers/ProductController.ts
import { Inject, Controller, Post, Body, Get, Query, Param } from '@midwayjs/core';
import { PublicationService } from '../../service/publication/publication.service';
import { Publication } from '../../entity/publication.entity';
import { v4 as uuid } from 'uuid';

@Controller('/api/publication')
export class PublicationController {
  @Inject()
  publicationService: PublicationService;

  @Post('/create')
  async create(@Body() publication: Publication): Promise<Publication> {
    // 添加 uuid
    publication.uuid = uuid();
    return this.publicationService.create(publication);
  }

  @Get('/all')
  async findAll(@Query('pageNo') pageNo: number, @Query('pageSize') pageSize: number): Promise<[Publication[], number]> {
    console.log(pageNo);
    return this.publicationService.findAll(pageNo, pageSize);
  }

  @Get('/detail/:uuid')
  async findOne(@Param('uuid') uuid: string): Promise<Publication> {
    return this.publicationService.findOne(uuid);
  }

  @Post('/update/:uuid')
  async update(@Param('uuid') uuid: string, @Body() publication: Partial<Publication>): Promise<Publication> {
    return this.publicationService.update(uuid, publication);
  }

  @Post('/remove/:uuid')
  async remove(@Param('uuid') uuid: string): Promise<void> {
    await this.publicationService.remove(uuid);
  }
}